import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/fans.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class MineFansController extends GetxController {
  RxList<Rx<FansModel>> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  void onInit() {
    pageList.add(Rx(FansModel(
        nickname: '如意家—每日上新',
        avatar:
        'https://img2.baidu.com/it/u=2654102281,3004956653&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=501',
        fans: 4599,
        city: '北海',
        count: 32)));
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 32) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    var r = Random.secure();
    for (var i = 0; i < 10; i++) {
      pageList.add(Rx(FansModel(
        nickname: '如意家—每日上新',
        avatar:
        'https://img0.baidu.com/it/u=3547779937,694625124&fm=253&fmt=auto&app=138&f=JPEG?w=285&h=285',
        fans: r.nextInt(99999),
        city: '北海',
        count: r.nextInt(100),
      )));
    }
  }
}

class MineFansPage extends GetView<MineFansController> {
  const MineFansPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: const Text('粉丝'),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return Container(
          height: 90,
          margin: const EdgeInsets.only(top: 20),
          padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
          decoration: const BoxDecoration(color: Colors.white),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CircleAvatar(
                radius: 25,
                backgroundImage: CachedNetworkImageProvider(row.value.avatar),
              ),
              Expanded(
                  child: Container(
                    padding: const EdgeInsets.only(left: 10),
                    child: Column(
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            row.value.nickname,
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                          ),
                        ),
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: const EdgeInsets.only(top: 10),
                          child: Text(
                            '粉丝 ${row.value.fans}',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color9999, fontSize: StylesUtils.fontSize13),
                          ),
                        )
                      ],
                    ),
                  )),
              Center(
                child: TextButton(
                    onPressed: () {
                      row.update((val) {
                        val?.checked = !val.checked;
                      });
                    },
                    style: ButtonStyle(
                        alignment: Alignment.center,
                        backgroundColor: MaterialStatePropertyAll(row.value.checked
                            ? ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048)
                            : Colors.transparent),
                        padding: const MaterialStatePropertyAll(EdgeInsets.zero),
                        minimumSize: const MaterialStatePropertyAll(Size(80, 30)),
                        fixedSize: const MaterialStatePropertyAll(Size(80, 30)),
                        shape: MaterialStatePropertyAll(RoundedRectangleBorder(
                            side: BorderSide(
                                width: row.value.checked ? 0 : 1,
                                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF048)),
                            borderRadius: BorderRadius.circular(5.r)))),
                    child: Text(
                      row.value.checked ? '关注' : '已关注',
                      style: StylesUtils.customTextStyle(
                          color: row.value.checked ? ColorsUtils.colorFfff : ColorsUtils.colorF048),
                    )),
              )
            ],
          ),
        );
      },
      itemCount: controller.pageList.length,
      itemExtent: 90,
    );
  }
}
